package com.itheima.leetcode;

/**
 * 删除排序链表中的重复元素
 *
 * @FileName E04LeetCode83
 * @Description
 * @Author Sword
 * @date 2025-03-20
 **/
public class E04LeetCode83 {

    //方法一
    public static ListNode deleteDuplicates1(ListNode head) {
        ListNode p1 = head;
        ListNode p2;

        while ((p2 = p1.next) != null) {
            //节点 < 2
            if (p1.val == p2.val) {
                //删除节点
                p1.next = p2.next;
            } else { //节点 > 2
                //向后平移
                p1 = p1.next;
            }
        }
        return head;
    }

    //方法二
    public static ListNode deleteDuplicates2(ListNode p) {
        if (p == null || p.next == null) {
            return p;
        }

        if (p.val == p.next.val) {
            return deleteDuplicates2(p.next);
        } else {
            p.next = deleteDuplicates2(p.next);
            return p;
        }
    }

    public static void main(String[] args) {
        ListNode listNode = ListNode.of(1, 1, 1, 2, 3, 3);
//        E04LeetCode83.deleteDuplicates1(listNode);
        ListNode p = E04LeetCode83.deleteDuplicates2(listNode);
        System.out.println(p);
    }
}
